package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.activity.LqBetListActivity;
import com.itboye.lottery.bean.BascketballBean;
import com.itboye.lottery.bean.FootballMatchBean;
import com.itboye.lottery.bean.ZqBetListItem;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class LqBetListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements CompoundButton.OnCheckedChangeListener {

    private ArrayList<ZqBetListItem> zqBetListItemArrayList;
    private Activity activity;
    private Context context;
    private boolean lotteryType;
    private int pager = 0;
    private ArrayList<FootballMatchBean> footballMatchBeanArrayList;

    public LqBetListAdapter(Activity activity, ArrayList<ZqBetListItem> zqBetListItemArrayList) {
        this.activity = activity;
        this.zqBetListItemArrayList = zqBetListItemArrayList;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(LqBetListAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setPager(int pager) {
        this.pager = pager;
        notifyDataSetChanged();
    }

    public void setOldArrayList(ArrayList<FootballMatchBean> footballMatchBeanArrayList) {
        this.footballMatchBeanArrayList = footballMatchBeanArrayList;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        try {
            int position = Integer.parseInt(compoundButton.getTag(R.id.tag_2) + "");
            String value = compoundButton.getTag(R.id.tag_1) + "";
            int isExit = isChoose(value, position);
            if (b) {
                if (pager == 1) {//非让分胜负
                    if (isExit == -1) {
                        ZqBetListItem.SpfBean spfBean = new ZqBetListItem.SpfBean();
                        spfBean.setKey(value.split("_")[0]);
                        spfBean.setValue(value.split("_")[1]);
                        zqBetListItemArrayList.get(position).getRfsfList().add(spfBean);
                    }
                } else if (pager == 2) {//让分胜负
                    if (isExit == -1) {
                        ZqBetListItem.SpfBean spfBean = new ZqBetListItem.SpfBean();
                        spfBean.setKey(value.split("_")[0]);
                        spfBean.setValue(value.split("_")[1]);
                        zqBetListItemArrayList.get(position).getFrsfList().add(spfBean);
                    }
                } else if (pager == 3) {//大小分
                    if (isExit == -1) {
                        ZqBetListItem.SpfBean spfBean = new ZqBetListItem.SpfBean();
                        spfBean.setKey(value.split("_")[0]);
                        spfBean.setValue(value.split("_")[1]);
                        zqBetListItemArrayList.get(position).getDxfList().add(spfBean);
                    }
                } else if (pager == 4) {//胜负差
                    if (isExit == -1) {
                        ZqBetListItem.SpfBean spfBean = new ZqBetListItem.SpfBean();
                        spfBean.setKey(value);
                        spfBean.setValue(value.split("_")[1]);
                        zqBetListItemArrayList.get(position).getSfcList().add(spfBean);
                    }
                }
            } else {
                if (pager == 1) {//胜平负
                    if (isExit != -1) {
                        zqBetListItemArrayList.get(position).getRfsfList().remove(isExit);
                    }
                } else if (pager == 2) {//让球胜平负
                    if (isExit != -1) {
                        zqBetListItemArrayList.get(position).getFrsfList().remove(isExit);
                    }
                } else if (pager == 3) {
                    if (isExit != -1) {
                        zqBetListItemArrayList.get(position).getDxfList().remove(isExit);
                    }
                } else if (pager == 4) {//胜负差
                    if (isExit != -1) {
                        zqBetListItemArrayList.get(position).getSfcList().remove(isExit);
                    }
                }
            }
        } catch (Exception e) {

        }
        Log.v("choose_data", zqBetListItemArrayList.toString());
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    int isChoose(String selectValue, int position) {
        int index = -1;
        if (pager == 1) {
            for (int i = 0; i < zqBetListItemArrayList.get(position).getRfsfList().size(); i++) {
                if (zqBetListItemArrayList.get(position).getRfsfList().get(i).getKey().contains(selectValue.split("_")[0])) {
                    index = i;
                    break;
                }
            }
        }
        if (pager == 2) {
            for (int i = 0; i < zqBetListItemArrayList.get(position).getFrsfList().size(); i++) {
                if (zqBetListItemArrayList.get(position).getFrsfList().get(i).getKey().contains(selectValue.split("_")[0])) {
                    index = i;
                    break;
                }
            }
        }
        if (pager == 3) {
            for (int i = 0; i < zqBetListItemArrayList.get(position).getDxfList().size(); i++) {
                if (zqBetListItemArrayList.get(position).getDxfList().get(i).getKey().contains(selectValue.split("_")[0])) {
                    index = i;
                    break;
                }
            }
        } else if (pager == 4) {
            for (int i = 0; i < zqBetListItemArrayList.get(position).getSfcList().size(); i++) {
                if (zqBetListItemArrayList.get(position).getSfcList().get(i).getKey().contains(selectValue)) {
                    index = i;
                    break;
                }
            }
        }
        return index;
    }

    public void clearAllStatus() {
//        for (int i = 0; i < codeBeanArrayList.size(); i++) {
//            if (codeBeanArrayList.get(i).isSelect()) {
//                codeBeanArrayList.get(i).setSelect(false);
//            }
//        }
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_lq_bet_list, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        ZqBetListItem zqBetListItem = zqBetListItemArrayList.get(position);
        viewHolder.item_zqbet_list_team.setText(zqBetListItem.gethTeam() + "VS" + zqBetListItem.getaTeam());
        viewHolder.item_zqbet_list_endtime.setText(zqBetListItem.getbDate() + " 截止");
        viewHolder.item_zqbet_list_delete.setTag(position);
        viewHolder.item_zqbet_list_delete.setOnClickListener((LqBetListActivity) activity);
        resetAllViews(viewHolder);
        switch (pager) {
            case 0://混投
                viewHolder.item_zqbet_list_huntou_spf_root.setVisibility(View.VISIBLE);
                addAllBet(zqBetListItem, viewHolder);
                break;
            case 1://非让分
                viewHolder.ll_frf_root.setVisibility(View.VISIBLE);
                addFrf(zqBetListItem, viewHolder, position, true);
                break;
            case 2://让分
                viewHolder.ll_frf_root.setVisibility(View.VISIBLE);
                addFrf(zqBetListItem, viewHolder, position, false);
                break;
            case 3://大小分
                viewHolder.ll_dxf_child_1.setVisibility(View.VISIBLE);
                addDxf(zqBetListItem, viewHolder, position);
                break;
            case 4://胜负差
                viewHolder.ll_sfc.setVisibility(View.VISIBLE);
                addSfc(zqBetListItem, viewHolder, position);
                break;
        }
    }


    private void setCheckBoxTag(CheckBox checkBox, int id, String tag) {
        checkBox.setTag(id, tag);
    }

    private void setCheckListener(CheckBox... checkBox0) {
        for (CheckBox checkBox : checkBox0) {
            checkBox.setOnCheckedChangeListener(this);
        }
    }

    private void removeCheckListener(CheckBox... checkBox0) {
        for (CheckBox checkBox : checkBox0) {
            checkBox.setOnCheckedChangeListener(null);
        }
    }


    private void addSfc(ZqBetListItem zqBetListItem, ViewHolder viewHolder, int position) {
        int gP = zqBetListItemArrayList.get(position).getgPosition();
        int cP = zqBetListItemArrayList.get(position).getcPosition();
        BascketballBean.WNMEntity wnmEntity = footballMatchBeanArrayList.get(gP).getBascketballBeanArrayList().get(cP).getWNM();
        String single = wnmEntity.getSingle();
        String[] sfcArray = activity.getResources().getStringArray(R.array.jclq_sfc);
//        key:4.20_3  value:1-5
        for (int i = 0; i < viewHolder.ll_sfc_zhusheng_odds.getChildCount(); i++) {//主胜 3
            CheckBox checkBox = ((CheckBox) viewHolder.ll_sfc_zhusheng_odds.getChildAt(i));
            String peilv = "";
            switch (i) {
                case 0:
                    peilv = wnmEntity.getSpecial_odds().getW1();
                    break;
                case 1:
                    peilv = wnmEntity.getSpecial_odds().getW2();
                    break;
                case 2:
                    peilv = wnmEntity.getSpecial_odds().getW3();
                    break;
                case 3:
                    peilv = wnmEntity.getSpecial_odds().getW4();
                    break;
                case 4:
                    peilv = wnmEntity.getSpecial_odds().getW5();
                    break;
                case 5:
                    peilv = wnmEntity.getSpecial_odds().getW6();
                    break;
            }

            StringBuilder tag = new StringBuilder(sfcArray[i] + "_3_" + peilv);
            checkBox.setTag(R.id.tag_1, tag);
            for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getSfcList()) {
                String tempValue = spfBean.getKey() + "_" + spfBean.getValue();
                if ((tempValue).equals(tag.toString())) {
                    checkBox.setChecked(true);
                    break;
                }
            }
            checkBox.setTag(R.id.tag_2, position);
            checkBox.setText(Html.fromHtml(sfcArray[i] + "<br />" + peilv));
            setCheckListener(checkBox);
        }


        for (int i = 0; i < viewHolder.ll_sfc_kesheng_odds.getChildCount(); i++) {//客胜 4
            String peilv = "";
            CheckBox checkBox = ((CheckBox) viewHolder.ll_sfc_kesheng_odds.getChildAt(i));
            switch (i) {
                case 0:
                    peilv = wnmEntity.getSpecial_odds().getL1();
                    break;
                case 1:
                    peilv = wnmEntity.getSpecial_odds().getL2();
                    break;
                case 2:
                    peilv = wnmEntity.getSpecial_odds().getL3();
                    break;
                case 3:
                    peilv = wnmEntity.getSpecial_odds().getL4();
                    break;
                case 4:
                    peilv = wnmEntity.getSpecial_odds().getL5();
                    break;
                case 5:
                    peilv = wnmEntity.getSpecial_odds().getL6();
                    break;
            }

            StringBuilder tag = new StringBuilder(sfcArray[i] + "_4_" + peilv);
            checkBox.setTag(R.id.tag_1, tag);
            for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getSfcList()) {
                String tempValue = spfBean.getKey() + "_" + spfBean.getValue();
                if ((tempValue).equals(tag.toString())) {
                    checkBox.setChecked(true);
                    break;
                }
            }
            checkBox.setTag(R.id.tag_2, position);
            checkBox.setText(Html.fromHtml(sfcArray[i] + "<br />" + peilv));
            setCheckListener(checkBox);
        }
    }

    private void addDxf(ZqBetListItem zqBetListItem, ViewHolder viewHolder, int position) {
        CheckBox checkBox0 = ((CheckBox) viewHolder.ll_dxf_child_1.getChildAt(0));
        CheckBox checkBox2 = ((CheckBox) viewHolder.ll_dxf_child_1.getChildAt(2));
        CheckBox checkBox1 = ((CheckBox) viewHolder.ll_dxf_child_1.getChildAt(1));
        setCheckListener(checkBox0, checkBox2);
        int gP = zqBetListItemArrayList.get(position).getgPosition();
        int cP = zqBetListItemArrayList.get(position).getcPosition();
        checkBox1.setEnabled(false);
        for (int i = 0; i < viewHolder.ll_dxf_child_1.getChildCount(); i++) {
            String big = footballMatchBeanArrayList.get(gP).getBascketballBeanArrayList().get(cP).getHILO().getH();//大分
            checkBox0.setText("大分 " + big);
            StringBuilder tag = new StringBuilder("大分_" + big);
            checkBox0.setTag(R.id.tag_1, tag);
            setCheckBoxTag(checkBox0, R.id.tag_2, position + "");

            String small = footballMatchBeanArrayList.get(gP).getBascketballBeanArrayList().get(cP).getHILO().getL();//小分
            checkBox2.setText("小分 " + small);
            tag = new StringBuilder("小分_" + small);
            checkBox2.setTag(R.id.tag_1, tag);
            setCheckBoxTag(checkBox2, R.id.tag_2, position + "");
            String score = footballMatchBeanArrayList.get(gP).getBascketballBeanArrayList().get(cP).getHILO().getFixedodds();//预估分
            checkBox1.setText(score);

        }
        for (int i = 0; i < zqBetListItem.getDxfList().size(); i++) {
            ZqBetListItem.SpfBean spfBean = zqBetListItem.getDxfList().get(i);
            if (spfBean.getKey().equals("大分")) {
                checkBox0.setChecked(true);
            } else if (spfBean.getKey().equals("小分")) {
                checkBox2.setChecked(true);
            }
        }
    }

    private void addFrf(ZqBetListItem zqBetListItem, ViewHolder viewHolder, int posistion, boolean isSpf) {
        ArrayList<ZqBetListItem.SpfBean> spfBeanArrayList = !isSpf ? zqBetListItem.getRfsfList() : zqBetListItem.getFrsfList();
        for (int i1 = 0; i1 < viewHolder.ll_frf_root.getChildCount(); i1++) {
            if (i1 == 1) {
                continue;
            }
            CheckBox checkBox = (CheckBox) viewHolder.ll_frf_root.getChildAt(i1);
            removeCheckListener(checkBox);
            int gP = zqBetListItemArrayList.get(posistion).getgPosition();
            int cP = zqBetListItemArrayList.get(posistion).getcPosition();
            String text = "";
            String str = "";
            BascketballBean.MNLEntity hadBean = isSpf ? footballMatchBeanArrayList.get(gP).getBascketballBeanArrayList().get(cP).getMNL() : footballMatchBeanArrayList.get(gP).getBascketballBeanArrayList().get(cP).getHDC();
            switch (i1) {
                case 0:
                    text = "主胜 " + hadBean.getH();
                    str = "主胜_" + hadBean.getH();
                    break;
                case 1:
                    text = "平 " + hadBean.getD();
                    str = "平_" + hadBean.getD();
                    break;
                case 2:
                    text = "客胜 " + hadBean.getA();
                    str = "客胜_" + hadBean.getA();
                    break;
            }
            checkBox.setText(text);
            StringBuilder tag = new StringBuilder(str);
            setCheckBoxTag(checkBox, R.id.tag_1, tag.toString());
            setCheckBoxTag(checkBox, R.id.tag_2, posistion + "");
            if (spfBeanArrayList != null) {
                for (ZqBetListItem.SpfBean bean : spfBeanArrayList) {
                    if ((bean.getKey() + "_" + bean.getValue()).equals(tag.toString())) {
                        checkBox.setChecked(true);
                        break;
                    } else if ((bean.getKey() + "_" + bean.getValue()).equals(tag.toString())) {
                        checkBox.setChecked(true);
                        break;
                    }
                }
            }
            setCheckListener(checkBox);
        }

    }

    private void addAllBet(ZqBetListItem zqBetListItem, ViewHolder viewHolder) {
        String str = "";
        for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getFrsfList()) {//胜负
            str += spfBean.getKey() + "(" + spfBean.getValue() + ")";
        }
        viewHolder.item_zqbet_list_spf.setVisibility(str.isEmpty() ? View.GONE : View.VISIBLE);
        viewHolder.item_zqbet_list_spf.setText(Html.fromHtml("非让分 " + "<font color='red'>" + str + "</font>"));
        str = "";
        for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getRfsfList()) {
            str += spfBean.getKey() + "(" + spfBean.getValue() + ")";
        }
        viewHolder.item_zqbet_list_rq.setVisibility(str.isEmpty() ? View.GONE : View.VISIBLE);
        viewHolder.item_zqbet_list_rq.setText(Html.fromHtml("让分 " + "<font color='red'>" + str + "</font>"));
        str = "";
        for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getDxfList()) {
            str += spfBean.getKey() + "(" + spfBean.getValue() + ") ";
        }
        viewHolder.item_zqbet_list_bf.setVisibility(str.isEmpty() ? View.GONE : View.VISIBLE);
        viewHolder.item_zqbet_list_bf.setText(Html.fromHtml("大小分 " + "<font color='red'>" + str + "</font>"));
        str = "";
        for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getSfcList()) {
            if (Integer.valueOf(spfBean.getKey().split("_")[1]) == 3) {
                str += spfBean.getKey().split("_")[0] + "(" + spfBean.getValue() + ")";
            }
        }
        viewHolder.item_zqbet_list_zjq.setVisibility(str.isEmpty() ? View.GONE : View.VISIBLE);
        viewHolder.item_zqbet_list_zjq.setText(Html.fromHtml("主胜 " + "<font color='red'>" + str + "</font>"));
        str = "";
        for (ZqBetListItem.SpfBean spfBean : zqBetListItem.getSfcList()) {
            if (Integer.valueOf(spfBean.getKey().split("_")[1]) == 4) {
                str += spfBean.getKey().split("_")[0] + "(" + spfBean.getValue() + ")";
            }
        }
        viewHolder.item_zqbet_list_bqc.setVisibility(str.isEmpty() ? View.GONE : View.VISIBLE);
        viewHolder.item_zqbet_list_bqc.setText(Html.fromHtml("客胜 " + "<font color='red'>" + str + "</font>"));
    }


    @Override
    public int getItemCount() {
        return zqBetListItemArrayList == null ? 0 : zqBetListItemArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView item_zqbet_list_delete;
        TextView item_zqbet_list_endtime, item_zqbet_list_team;

        TextView item_zqbet_list_spf, item_zqbet_list_rq, item_zqbet_list_bf, item_zqbet_list_zjq, item_zqbet_list_bqc;
        TextView item_zqbet_list_bf_s, item_zqbet_list_bf_f, item_zqbet_list_bf_p;
        LinearLayout item_zqbet_list_huntou_spf_root, ll_frf_root, ll_dxf_child_1, ll_sfc_kesheng_odds, ll_sfc_zhusheng_odds;
        LinearLayout ll_bqc_child_1, ll_bqc_child_2, ll_bqc_child_3, ll_sfc;

        public ViewHolder(View itemView) {
            super(itemView);
            item_zqbet_list_delete = itemView.findViewById(R.id.item_zqbet_list_delete);
            item_zqbet_list_team = itemView.findViewById(R.id.item_zqbet_list_team);
            item_zqbet_list_endtime = itemView.findViewById(R.id.item_zqbet_list_endtime);
            item_zqbet_list_spf = itemView.findViewById(R.id.item_zqbet_list_spf);
            item_zqbet_list_rq = itemView.findViewById(R.id.item_zqbet_list_rq);
            item_zqbet_list_bf = itemView.findViewById(R.id.item_zqbet_list_bf);
            item_zqbet_list_zjq = itemView.findViewById(R.id.item_zqbet_list_zjq);
            item_zqbet_list_bqc = itemView.findViewById(R.id.item_zqbet_list_bqc);
            item_zqbet_list_huntou_spf_root = itemView.findViewById(R.id.item_bq_bet_list_huntou_root);
            ll_frf_root = itemView.findViewById(R.id.ll_frf_root);
            ll_dxf_child_1 = itemView.findViewById(R.id.ll_dxf_child_1);
            ll_sfc_kesheng_odds = itemView.findViewById(R.id.ll_sfc_kesheng_odds);
            ll_sfc_zhusheng_odds = itemView.findViewById(R.id.ll_sfc_odds);
            item_zqbet_list_bf_s = itemView.findViewById(R.id.item_zqbet_list_bf_s);
            item_zqbet_list_bf_f = itemView.findViewById(R.id.item_zqbet_list_bf_f);
            item_zqbet_list_bf_p = itemView.findViewById(R.id.item_zqbet_list_bf_p);
            ll_bqc_child_1 = itemView.findViewById(R.id.ll_bqc_child_1);
            ll_bqc_child_2 = itemView.findViewById(R.id.ll_bqc_child_2);
            ll_bqc_child_3 = itemView.findViewById(R.id.ll_bqc_child_3);
            ll_sfc = itemView.findViewById(R.id.ll_sfc);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }


    private void resetAllViews(ViewHolder holder) {
        holder.item_zqbet_list_huntou_spf_root.setVisibility(View.GONE);
        holder.ll_frf_root.setVisibility(View.GONE);
        holder.ll_dxf_child_1.setVisibility(View.GONE);
        holder.ll_sfc.setVisibility(View.GONE);
    }
}
